import java.net.URL;

import com.vmware.vim25.AuthorizationDescription;
import com.vmware.vim25.AuthorizationPrivilege;
import com.vmware.vim25.AuthorizationRole;
import com.vmware.vim25.ElementDescription;
import com.vmware.vim25.mo.AuthorizationManager;
import com.vmware.vim25.mo.ServiceInstance;

/**
 * http://vijava.sf.net
 * @author Steve Jin
 */

public class ListAuthorization
{
  public static void main(String[] args) throws Exception
  {
	  String[] temp = new String[]{"https://10.16.2.59/sdk","root","vmware"};
	    ServiceInstance si = new ServiceInstance(
	        new URL(temp[0]), temp[1], temp[2], true);

    AuthorizationManager am = si.getAuthorizationManager();
    AuthorizationDescription ad = am.getDescription();
    
    System.out.println("Descriptions of all privileges:");
    printDescriptions(ad.getPrivilege());

    System.out.println("Descriptions of all privilege groups:");
    printDescriptions( ad.getPrivilegeGroup());

    System.out.println("List of all privileges with details:");
    printPrivileges(am.getPrivilegeList());
    
    System.out.println("List of all the roles with details:");
    printRoles(am.getRoleList());
    
    si.getServerConnection().logout();
  }
  
  static void printDescriptions(ElementDescription[] eds)
  {
    for(int i=0; eds!=null && i< eds.length; i++)
    {
      System.out.println("\nKey:" + eds[i].getKey());
      System.out.println("Label:" + eds[i].getLabel());
      System.out.println("Summary:" + eds[i].getSummary());
    }
  }
  
  static void printPrivileges(AuthorizationPrivilege[] aps)
  {
    for(int i=0; aps!=null && i< aps.length; i++)
    {
      System.out.println("\nName:" + aps[i].getName());
      System.out.println("onParent:" + aps[i].isOnParent());
      System.out.println("Group:" + aps[i].getPrivGroupName());
      System.out.println("ID:" + aps[i].getPrivId());
    }
  }

  static void printRoles(AuthorizationRole[] ars)
  {
    for(int i=0; ars!=null && i<ars.length; i++)
    {
      System.out.println("\nName:" + ars[i].getName());
      System.out.println("Role ID:" + ars[i].getRoleId());
      System.out.println("Sys Role:" + ars[i].isSystem());
      System.out.print("Privileges: ");
      String[] privs = ars[i].getPrivilege();
      for(int j=0; privs!=null && j < privs.length; j++)
      {
        if(j==0)
        {
          System.out.print(privs[j]);
        }
        else
        {
          System.out.print(", " + privs[j]);
        }
      }
    }
  }
}